package com.visionblue.business.ship.page.orders

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.visionblue.business.ship.R
import com.visionblue.business.ship.base.BaseFragment
import com.visionblue.business.ship.base.DefaultObserver
import com.visionblue.business.ship.model.bean.response.OrdersReceiveInfo
import com.visionblue.business.ship.util.toast
import kotlinx.android.synthetic.main.dialog_selection.*
import kotlinx.android.synthetic.main.frag_orders_receive.*
import org.koin.androidx.viewmodel.ext.android.sharedViewModel

/**
 * des:接收联单
 * author: ysj
 * date: 2021/3/29
 */
open class OrdersReceiveFragment: BaseFragment(),View.OnClickListener, BaseQuickAdapter.OnItemChildClickListener{
    private lateinit var viewEmpty: View
    private var mPageNum: Int = 1
    private var isRefresh: Boolean = true
    private lateinit var  mAdapterReceive:OrderReceiveListAdapter
    private val mList = ArrayList<OrdersReceiveInfo>()
    protected val mViewModel: OrderViewModel by sharedViewModel()

    override val layoutId: Int
        get() = R.layout.frag_orders_receive

    override fun initView(savedInstanceState: Bundle?) {
        initRefresh()
        mAdapterReceive.onItemChildClickListener =this
    }

    private fun initRefresh() {
        viewEmpty = LayoutInflater.from(activity).inflate(R.layout.view_empty,null)
        viewEmpty.setOnClickListener(this)

        mAdapterReceive = OrderReceiveListAdapter(R.layout.item_receive_order)
        mAdapterReceive.emptyView = viewEmpty
        rv_receive.layoutManager = LinearLayoutManager(context)
        rv_receive.adapter = mAdapterReceive

        srl_receive?.setDisableContentWhenRefresh(true)
        srl_receive?.setDisableContentWhenLoading(true)
        srl_receive?.autoRefresh()
        srl_receive?.setEnableLoadMore(true)

        srl_receive?.setOnRefreshLoadMoreListener(object:OnRefreshLoadMoreListener{
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mPageNum++
                isRefresh = false
                getListData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                mPageNum = 1
                isRefresh = true
                getListData()
            }

        })

        data()

    }

    private fun getListData() {
       mViewModel.getOrdersReceiveList(mPageNum)
    }

    private fun data() {
       mViewModel.orderReceiveListResult.observe(this,DefaultObserver(action = {
           srl_receive?.finishRefresh()
           srl_receive?.finishLoadMore()
           if (it.data!=null){
               val content = it.data?.content?:return@DefaultObserver
               //tv_page.text = "第"+mPageNum.toString()+"页"

               if(isRefresh){
                   mList.clear()
                   if(content!=null&& content.isNotEmpty()){
                       mList.addAll(content)
                   }
                   mAdapterReceive.replaceData(mList)
               }else{
                   mList.addAll(content)
                    mAdapterReceive.addData(content)
               }

               if(content.size<10){
                   srl_receive?.setNoMoreData(true)
               }
           }
       }))
    }

    override fun onClick(v: View?) {
        when(v){
            viewEmpty->{
                getListData()
            }
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val data = adapter?.data?.get(position) as OrdersReceiveInfo
        when(view?.id){
            R.id.ll_btn_check ->{
                PdfActivity.start(activity!!,data.pdfUrl)
            }
        }
    }

}